import http from "@ohos.net.http";
import router from "@system.router";
import prompt from "@system.prompt";

export default {
    props: {
        // 由注册页传参至此
        sms: {
            default: {
                id: 10,
                phone: "",
                code: "",
            },
        },
    },
    onShow() {
        console.info("!! this.sms = " + JSON.stringify(this.sms));
        if (this.sms?.phone?.length) {
            // 模拟短信
            prompt.showDialog({
                message: `您正在申请修改密码，您的验证码：${this.sms.code}。请在5分钟内输入完成验证。`,
            });
        }
    },
    onInit() {
        // this.$watch('activeInput', 'watchInput');
        this.startTimer();
    },
    data: {
        activeInput: 0, // 激活的输入框索引
        codes: [
            {
                num: "",
            },
            {
                num: "",
            },
            {
                num: "",
            },
            {
                num: "",
            },
            {
                num: "",
            },
            {
                num: "",
            },
        ], // 6位数
        timer: 60, // 验证码获取计时
    },
    computed: {
        codeText() {
            return this.timer > 0 ? `${this.timer}s后` : "";
        },
    },
    startTimer() {
        this.timer = 60;
        let timer = setInterval(() => {
            if (this.timer == 0) {
                clearInterval(timer);
                return;
            }
            this.timer--;
        }, 1000);
    },
    onCodeInput(ev) {
        console.info("!! " + JSON.stringify(ev));
        console.info("!! id:" + ev.target.id);
        // todo
        this.codes[this.activeInput].num = ev.value;
        console.info("!! codes" + JSON.stringify(this.codes));
        let realCode = this.codes.map(a => a.num).join("");
        console.info("!! realCode=" + realCode);
        // 如果长度不为零，说明是输入，聚焦到下一个输入框，否则回退
        this.activeInput = ev.value?.length
            ? Math.min(5, this.activeInput + 1)
            : Math.max(0, this.activeInput - 1);
        this.$element("code-" + this.activeInput)?.focus();
        // ! 以上只能在填完 6 个数后才能回退，因为没法监听退格键

        // 输入第 6 位后校验验证码，（注册）
        if (ev.target.id == "code-5" && this.activeInput == 5) {
            this.testCode();
        }
    },
    testCode() {
        // 校验验证码
        let realCode = this.codes.map(a => a.num).join("");
        console.info("!! realCode=" + realCode + "  sms:" + this.sms?.code);
        if (realCode !== this.sms?.code) {
            prompt.showToast({
                // @ts-ignore
                message: "验证码错误",
            });
            return;
        }
        // 接下来注册
        const req = http.createHttp();
        const promise = req.request("https://trezedo.club/api/ha/user", {
            method: "POST",
            extraData: this.sms,
        });
        promise.then(({ result }) => {
            console.info("!! " + JSON.stringify(result));
            result = JSON.parse(result);
            // @ts-ignore
            if (result.code == 200) {
                // 此处用 push 才有动画
                router.replace({
                    uri: "pages/login_register/pwd/pwd",
                    params: {
                        phone: this.sms?.phone,
                    },
                });
            } else {
                prompt.showToast({
                    // @ts-ignore
                    message: result.msg,
                });
            }
        });
    },
    // 校准输入顺序
    adjust(index) {
        console.info("!!" + index);
        let dom = this.$element("code-" + this.activeInput);
        if (index !== this.activeInput && dom) {
            dom.focus();
        }
    },
    watchInput(n, o) {
        console.info("!! " + n);
    },
    resendCode() {
        this.startTimer();
        // 重新发送
        const req = http.createHttp();
        const promise = req.request("https://trezedo.club/api/ha/user/code", {
            method: "POST",
            extraData: {
                phone: this.sms?.phone,
            },
        });
        promise.then(({ result }) => {
            console.info("!! " + result);
            result = JSON.parse(result);
            // @ts-ignore
            if (result.code == 200) {
                prompt.showDialog({
                    // @ts-ignore
                    message: `您正在申请修改密码，您的验证码：${result.data.code}。请在5分钟内输入完成验证。`,
                });
            } else {
                prompt.showToast({
                    // @ts-ignore
                    message: result.msg,
                });
            }
        });
    },
};
